package com.systop.asset.controller;

import java.security.Principal;
import java.util.List;

import com.systop.asset.entity.RestResponse;
import com.systop.asset.service.IUserService;
import com.systop.asset.entity.User;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统用户基础信息Controller
 *
 * @date 2025-06-20
 */
@RestController
@RequestMapping("/system/user")
public class UserController
{
    @Autowired
    private IUserService userService;

    /**
     * 查询系统用户基础信息列表
     */
    @GetMapping("/list")
    public RestResponse list(User user)
    {
        List<User> list = userService.selectUserList(user);
        System.out.println(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        return RestResponse.success(list);
    }

    /**
     * 查询系统用户基础信息列表
     */
    @GetMapping("/listall")
    public RestResponse listall(User user)
    {
        List<User> list = userService.selectUserList(user);
        System.out.println(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        return RestResponse.success(list);
    }


    /**
     * 获取系统用户基础信息详细信息
     */
    @GetMapping(value = "/{id}")
    public RestResponse getInfo(@PathVariable("id") Long id)
    {
        return RestResponse.success(userService.selectUserById(id));
    }

    /**
     * 新增系统用户基础信息
     */
    @PostMapping
    public RestResponse add(@RequestBody User user)
    {
        return RestResponse.success(userService.insertUser(user));
    }

    /**
     * 修改系统用户基础信息
     */
    @PutMapping
    public RestResponse edit(@RequestBody User user)
    {
        return RestResponse.success(userService.updateUser(user));
    }

    /**
     * 删除系统用户基础信息
     */
	@DeleteMapping("/{ids}")
    public RestResponse remove(@PathVariable Long[] ids)
    {
        return RestResponse.success(userService.deleteUserByIds(ids));
    }

    /**
     *  根据用户名查询用户信息
     * @param username
     * @return
     */
    @GetMapping("/getUser")
    public RestResponse getUserByName(String username)
    {
        return RestResponse.success(userService.selectUserByUsername(username));
    }
}
